$(function () {
    loadKeyWords();
    // loadTable();
});
var table;

function loadKeyWords() {
    var url = baseQuestionUrl + "list/greet?start=0&limit=1000000";
    ajaxCommon.request('get',url,{},function (data) {
        loadTable(data.data.rows);
    })

}

function loadTable(data) {
    var jishuji=0;
    var obj={
        data: data,
        columns: [
            {data: ''},
            {data: ''},
        ],
        columnDefs: [
            {
                targets: 1,
                render: function (data, type, row, meta) {
                    return "<div name='disp' id='disp"+row.id+"'>"+row.greeting+"</div><textarea name='greet' id='greeting"+row.id+"' rows='3'  style='width:600px;ertical-align:top;display:none;'>";
                }
            },
            {
                targets: 0,
                render: function (data, type, row, meta) {
                    jishuji=jishuji+1;
                    return jishuji;
                }
            },
            {
                targets: 2,
                render: function (data, type, row, meta) {
                    return formatDate1(row.updated)
                }
            },
            {
                targets: 3,
                render: function (data, type, row, meta) {
                    return "<button name='a' id='a"+row.id+"' type='button' style='margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;'  class='btn btn-primary' onclick='deleteQuestion1(" + JSON.stringify(row) +");'>编辑</button><button name='b' id='b"+row.id+"'type='button' style='display:none;margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;' class='btn btn-primary' onclick='updateKeyWord(" + JSON.stringify(row) +");'>确定</button><button name='b' id='c"+row.id+"'  type='button' style='display:none;margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;' class='btn btn-primary' onclick='quxiaoanniu(" + JSON.stringify(row) +")'>取消</button><button name='a'  id='d"+row.id+"'  type='button' class='btn btn-danger' style='margin-right: 10px;border: 1px solid red;color: red;background: transparent;' onclick='deleteQuestion(" + JSON.stringify(row) + ");'>删除</button>";
                }
            }]
    }
    ajaxCommon.loadTableData('#keyWordsTable',obj)
}

/**
 * 新增问候语
 */
function xinzenwenhou() {
    var data = {"keyWord": $('#greetings').val()};
    var url = baseQuestionUrl + "add/greet";
    ajaxCommon.request('post',url,JSON.stringify(data),function (data) {
        if (!data.data) {
            swal("新增失败",'','error');
        } else {
            swal('新增成功','','success');
            $('#deleteModal2').modal('hide');
            loadKeyWords();
        }
    })
    $('#greetings').val("")
}

function showDelete(keyWord) {
    $("#id").text(keyWord.id);
    $('#deleteModal').modal('show');
}

/*
* 编辑问候语
* */
function updateKeyWord(wenhou){
    url = baseQuestionUrl + "update1";
    var woshizhu= $("#greeting"+wenhou.id).val();
    var greeting = new Greeting(wenhou.id, woshizhu);
    var data = JSON.stringify(greeting);
    ajaxCommon.request('post',url,data,function (data) {
        if (!data.data) {
            swal("修改失败",'','error');
        } else {
            swal('修改成功','','success');
            loadKeyWords();
        }
    })
}
/**
 * 删除一个未启用的关键字
 * @param id
 */
function deleteKeyWord(id) {
    var url = baseQuestionUrl + "delete1/" + id;
    ajaxCommon.request('delete',url,{},function () {
        loadKeyWords();
        $('#deleteModal').modal('hide');
    })
}
function deleteQuestion(question) {
    swal({
            title: "删除未匹配问题",
            text: "确认要删除未匹配问题？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#337ab7",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false,
            closeOnCancel: true
        },
        function(isConfirm){
            if (isConfirm) {
                var url = baseQuestionUrl + "delete1/" + question.id;
                ajaxCommon.request('delete',url,{},function (res) {
                    if(res.code===200) {
                        swal("删除！", "当前记录已被删除！", "success");
                        loadKeyWords()
                        // table.ajax.reload();
                    }
                    // $('#deleteModal').modal('hide');
                })
            }
        });
}
function deleteQuestion1(question) {
    $("button[name*='a']").show();
    $("button[name*='b']").hide();
    $("#b"+question.id).show();
    $("#c"+question.id).show();;
    $("#d"+question.id).hide();
    $("#a"+question.id).hide();
    $("textarea[name*='greet']").hide();
    $("div[name*='disp']").show();
    $("#greeting"+question.id).show();
    $("#greeting"+question.id).val(question.greeting);
    $("#disp"+question.id).hide();
}
function deleteQuestion2(question) {
    $('#deleteModal2').modal('toggle');
}
/*页面取消*/
function  quxiaoanniu(question){
    $("#b"+question.id).hide();
    $("#c"+question.id).hide();
    $("#d"+question.id).show()
    $("#a"+question.id).show()
    $("#greeting"+question.id).hide();
    $("#greeting"+question.id).val(question.greeting);
    $("#disp"+question.id).show();
}
